package com.gd.iot.energy.config.shiro;

import com.gd.iot.energy.filter.SimpleFormAuthenticationFilter;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @author kris
 */
@Configuration
public class ShiroConfig{

    @Bean
    @ConditionalOnMissingBean
    public DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator() {
        DefaultAdvisorAutoProxyCreator defaultAAP = new DefaultAdvisorAutoProxyCreator();
        defaultAAP.setProxyTargetClass(true);
        return defaultAAP;
    }


    /**
     * 将自己的验证方式加入容器
     * @return
     */
    @Bean
    public CustomRealm myShiroRealm() {
        CustomRealm customRealm = new CustomRealm();
        return customRealm;
    }

    /**
     * 将自己的验证方式加入容器
     * @return
     */
    @Bean
    public CustomDefaultWebSessionManager customDefaultWebSessionManager() {
        CustomDefaultWebSessionManager customDefaultWebSessionManager = new CustomDefaultWebSessionManager();
        return customDefaultWebSessionManager;
    }

    @Bean
    public DefaultWebSecurityManager securityManager(CustomDefaultWebSessionManager customDefaultWebSessionManager) {
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        securityManager.setRealm(myShiroRealm());
        //shiro 的session默认放在cookie中 禁用
        customDefaultWebSessionManager.setSessionIdCookieEnabled(false);
        //禁用url 重写 url; shiro请求时默认 jsessionId=id
        customDefaultWebSessionManager.setSessionIdUrlRewritingEnabled(false);
        //禁止Subject的getSession方法
        securityManager.setSessionManager(customDefaultWebSessionManager);
        return securityManager;
    }

    /**
     * Filter工厂，设置对应的过滤条件和跳转条件
     * @param securityManager
     * @return
     */
    @Bean
    public ShiroFilterFactoryBean shiroFilterFactoryBean(SecurityManager securityManager) {
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        shiroFilterFactoryBean.setSecurityManager(securityManager);
        Map<String, String> map = new HashMap<>();
        //登出
        map.put("/admin/logout", "logout");
        //需要验证的
        map.put("/**", "authc");
        //不需要验证的
        map.put("/admin/login","anon");
        map.put("/admin/logout","anon");
        map.put("/admin/kaptcha","anon");
        //swagger接口权限 开放
        map.put("/swagger-ui.html", "anon");
        map.put("/webjars/**", "anon");
        map.put("/v2/**", "anon");
        map.put("/swagger-resources/**", "anon");
        map.put("/ctwing/subscribe/**", "anon");
        shiroFilterFactoryBean.getFilters().put("authc",new SimpleFormAuthenticationFilter());
        shiroFilterFactoryBean.setFilterChainDefinitionMap(map);
        return shiroFilterFactoryBean;
    }

    @Bean
    public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(SecurityManager securityManager) {
        AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor =
                new AuthorizationAttributeSourceAdvisor();
        authorizationAttributeSourceAdvisor.setSecurityManager(securityManager);
        return authorizationAttributeSourceAdvisor;
    }
}